package com.gzy.gameserver;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.gamalocus.sgs.services.mysql.MySQLManager;
import com.sun.sgs.app.AppContext;

public class MissionManager {

	private static final MissionManager s_missionMgr = new MissionManager();
	protected Map<Integer, Mission.MissionData> missionMap = new HashMap<Integer, Mission.MissionData>();
	private static final Logger logger = Logger.getLogger(Pawn.class.getName());
	
	public MissionManager()
	{
		this.initialize();
	}
	
	public static MissionManager sharedManager()
	{
		return s_missionMgr;
	}
	
	protected boolean initialize()
	{
		try
		{
			MySQLManager myman = AppContext.getManager(MySQLManager.class);
			ResultSet res = myman.executeSelect("SELECT id from magics");
			
			while(res.next())
			{
				String strId = res.getString("id");
				ResultSet missionRes = myman.executeSelect("SELECT * from missions WHERE id=" + strId);
				if(missionRes.next())
				{
					Integer id = new Integer(strId);
					
					Mission.MissionData mission = new Mission.MissionData(id.intValue(), missionRes.getInt("steps"));
					this.missionMap.put(id, mission);
				}
			}
		}
		catch(Exception e)
		{
			logger.log(Level.INFO, "{0}", e);
		}
		return true;
	}
	
	public Mission spawnMission(int id)
	{
		Mission.MissionData missionData = this.missionMap.get(new Integer(id));
		return new Mission(missionData.m_id, missionData.m_steps);
	}
}
